home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / U-Z / VideoToolBox Folder / Notes / Grayscale Information < prev    next >
Encoding:
Text File  |  1993-01-07  |  12.2 KB  |  226 lines  |  [TEXT/ttxt]

  1. Grayscale Information
  2.  
  3. Article Created: 23 April 1989
  4. Article Last Reviewed: 6 July 1992
  5. Article Last Updated:
  6.  
  7. TOPIC -----------------------------------------------
  8.  
  9. A user has found (using very sophisticated photometric devices) that when
  10. he generates a monochrome palette from a color palette, some of the tones
  11. are identical.  This article discusses this phenomenon.
  12.  
  13. DISCUSSION ------------------------------------------
  14.  
  15. In regard to using the color palette, the Palette Manager does not place
  16. new colors into the display device's color look-up table (CLUT).  It maps
  17. the requested colors in a palette to available colors within the current
  18. color table.  The correct method for using specific color values is to use
  19. the Color Manager calls to create and load a programmed color table.
  20.  
  21. There are some other points that must taken into account after setting
  22. your colors with the Color Manager.
  23.  
  24. - It is possible to produce grays by setting the monitor to monochrome
  25. output and not setting grays, as the user in question probably does.  When
  26. selecting monochrome from the monitor's Control Panel CDEV, luminance
  27. mapping of the colors in the color table is done to calculate and replace
  28. the RGB values for a monochrome pixel.  The luminance mapping follows this
  29. equation:
  30.  
  31.    Luminance = .3*red + .59*green + .11*blue
  32.  
  33. The resulting value is placed into the display device's table for R, G,
  34. and B.  The values are altered according to the above equation. This needs
  35. to be taken into account if the monochrome option is chosen for the Apple
  36. High-Resolution Color Monitor.
  37.  
  38. - If you place your own RGB "gray" values into a color table, you must
  39. also take into account gamma correction. Gamma correction is done by the
  40. display device's driver to correct the disproportionate light output of
  41. the CRT phosphors by comparison with the proportional input voltages of
  42. the video signal.  To set a linear relationship between colors and output,
  43. the values set for the Macintosh II Video Card are changed to new values,
  44. which then produce the corrected color output on the monitor. Test
  45. equipment detects a gamma-filtered set of values from the monitor.
  46.  
  47. The gamma correction is automatically handled for all values written to
  48. the device's color table.  This is based on an empirically-derived gamma
  49. look-up table. If the selected values fall within the same look-up table
  50. reference, they can be mapped to the same gamma value.  This would explain
  51. why some values are the same.   The gamma table ID is located in the SCRN
  52. resource of the System file for the Macintosh II Video Card. A new GAMA
  53. Resource table can be defined and installed in the System file for use by
  54. the Macintosh II Video Card. To use the new gamma table, the GAMA resource
  55. ID must be changed in the SCRN resource to that of the new table.
  56.  
  57. The safest values for any testing will probably be a linear table that
  58. does no gamma correction.  A quick and easy way to do this is to pass -1
  59. [dgp: according to Designing Cards & Drivers you should pass NULL, not -1] to the SetGamma Control parameter block as the new table address.  This
  60. causes a linear table to be used, guaranteeing that all of the possible
  61. gray levels of the card are available.  At that point, the actual color
  62. output on the monitor depends on the phosphors themselves, which have a
  63. variance from monitor to monitor of +/-.02 for the red and green phosphors
  64. and +/-.015 for the blue. The CIE coordinates of the phosphors are:
  65.  
  66.    Red: x=.625 y=.34 Green: x=.28   y=.595 Blue:     x=.155 y=.070
  67.  
  68. To properly set the gamma table, see the information below about how the
  69. driver uses this table.  You should also investigate how to control device
  70. drivers. This is described in "Inside Macintosh Volume II." Another source
  71. of information is the "Designing Cards and Drivers Manual for the
  72. Macintosh."
  73.  
  74. Gamma Information
  75. -----------------
  76. In the current implementation of the video drivers, gamma correction is
  77. applied to requested, absolute colors, immediately before they are set in
  78. the color look-up table by the SetEntries control call. More specifically,
  79. some number of high-order bits are extracted from the red, green, and blue
  80. channels and used as an index into tables of corrected values.  These
  81. values are then placed into the hardware, yielding corrected output. On
  82. the Macintosh II Video Card (the TFB card), the high eight bits of each
  83. channel are used to reference the gamma table.
  84.  
  85. There are a number of minor shortcomings in this implementation. First,
  86. there is not absolute symmetry between the SetEntries control call, which
  87. sets the CLUT, and the GetEntries status call, which reads the CLUT
  88. hardware, because the gamma correction took place as part of the
  89. SetEntries call. Also, the uncorrected values are generally unrecoverable
  90. (although a copy of the absolute colors are always available in the
  91. GDevice structure).
  92.  
  93. Finally, it is most desirable to extract more bits as an index to the
  94. gamma table than the number of bits of color information that will be set
  95. in the CLUT.  This way, you avoid a loss of color resolution after
  96. correction. For example, the TFB card has an eight-bit-per-channel CLUT,
  97. but only uses the most-significant eight bits of the (16-bit) channel
  98. information for gamma lookup. At lower intensities, the gamma correction
  99. increases the distance between adjacent values.  As a result, on the TFB
  100. card, some dynamic range is lost at lower intensities. This could be
  101. corrected by extracting nine or ten bits of channel information rather
  102. than eight and using a larger, gamma-correction table, but this option was
  103. declined to reduce gamma table size.
  104.  
  105. The GammaTbl Data Structure
  106. ---------------------------
  107. [dgp: This section has many errors. I suggest you turn instead to Designing Cards and Drivers, 3rd Ed., which, finally, includes this information.]
  108.  
  109. The structure itself has been a bit of a mystery, as it is not defined in
  110. either "Inside Macintosh Volume V" or the Designing Cards and Drivers
  111. Manual. This is the structure:
  112.  
  113.    record GammaTable of
  114.    gVersion: integer; {gtab version, currently 0}
  115.    gType: integer; {drHwId value}
  116.    gFormulaSize: integer; {size of formula data, below}
  117.    gChanCnt: integer; {# of component channels}
  118.    gDataCnt: integer; {# of values per channel}
  119.    gDataWidth: integer; {size of data in tables}
  120.    {gamma correction look-up tables}
  121.    gFormulaData: array [0.. gFormulaSize] of byte;
  122.    {data for gamma calculation formula}
  123.    gData: array [0.. gDataCnt] of byte;
  124.    end;
  125.  
  126. In this structure, gVersion represents the gamma table format version,
  127. which is 0 for all current cards.  The gType field holds the drHwId value
  128. for this board to identify the board that this table was measured for.
  129. Note that this means that a single gamma table can't directly be shared
  130. between two different cards, even if they both have the same CLUT response
  131. curve (which is usually linear).  This lets the data in the gamma table be
  132. in an appropriate form for varying hardware (that is, a card could have
  133. four-bit/channel DACs and might prefer gamma data in the range $0..$F
  134. rather than $0..$FF).
  135.  
  136. gFormulaSize defines the number of bytes occupied by the gFormulaData
  137. field. On Apple's current video cards, gamma correction is done by
  138. modifying the values loaded into the CLUT by the SetEntries control call
  139. to approximate linear response on the display.  On these systems, the
  140. gamma correction table acts as a final, look-up, data table, which
  141. translates the requested color into closest available linearized level.
  142. These gamma table values are determined empirically by measuring the
  143. output of a calibrated display.
  144.  
  145. More sophisticated systems may choose an alternative to this simple
  146. look-up mechanism.  For instance, you can calculate gamma correction
  147. factors based on a mathematical response function. By default, the TFB
  148. card uses a single correction table for all three channels. No
  149. calculations are performed on the incoming color table other than simple
  150. lookup.  Cards can remember the specific monitor configuration at the
  151. beginning of the gFormulaData field, allowing it to identify and use only
  152. gamma tables developed for the currently connected monitor.
  153.  
  154. gChanCnt is the number of look-up tables in gData, below. If there is more
  155. than one channel of gamma correction data, then the R, G, and B tables
  156. follow each other, respectively, at the end of the structure.
  157.  
  158. gDataCnt is the number of discrete look-up values included in each of the
  159. channel's correction table.  It is always equal to 2gDataWidth [dgp: I think they meant to say 1<<gDataWidth], but refers to number of bytes that this channel's data occupies. 
  160.  
  161. gDataWidth describes the number of significant bits of information
  162. available in each entry in a channel's correction table. The data always
  163. appears as gDataWidth bits, right-justified in a field that is the next
  164. larger number of bytes than gDataWidth.  Because it is rare to have
  165. devices with more than eight bits of CLUT resolution, virtually all
  166. devices pack their correction data into bytes.
  167.  
  168. gData is actual correction table data. If there is more than one channel's
  169. information, each table follows the next in R, G, B order. The standard
  170. tables included in Apple's driver have only one table, which is applied to
  171. all three output channels.  Because Pascal cannot express variable size
  172. fields in record structures, the independent channels are not individually
  173. named.
  174.  
  175. "gama" Resource Format
  176. ----------------------
  177. In addition to the RAM data structure for gamma tables covered above,
  178. there is a standard resource format for gamma table resources.  Like many
  179. other resource templates, the gamma structure is an image of the RAM form
  180. stored in resource format. There are no changes.
  181.  
  182. Using Gamma Correction
  183. ----------------------
  184. Gamma correction is always applied by the TFB video driver. At driver open
  185. time, the driver is usually initialized with a linear (noncorrecting)
  186. gamma table.  When _InitGraf is called, the "scrn" screen configuration
  187. resource is read from the System file. This resource (described in "Inside
  188. Macintosh Volume V") includes information about the size and orientation
  189. of the different monitors configured into the system, including their last
  190. video mode (pixelsize), color table, and gamma table.
  191.  
  192. If there is no "gama" resource ID specified, or the specified ID is not
  193. present, then a default gamma table, "gama" =0 is loaded from the System
  194. file and used (this is the table calculated for the TFB card). If the
  195. specified resource is found, then the appropriate resource is loaded, and
  196. a control call is issued to the driver to make this the current gamma
  197. table. Unfortunately, there is currently no tool to set the "gama" ID,
  198. short of modifying the "scrn" resource directly.
  199.  
  200. To facilitate the use of the gamma table, there are two calls in the
  201. standard, video-driver routines that set the gamma table (control call 4,
  202. SetGamma) and retrieve the pointer to the current gamma table (status call
  203. 6 [dgp: oops, they mean 8] on TFB rev 2 drivers and up). These calls simply take and return a pointer to a GammaTbl structure.
  204.  
  205.  
  206. Copyright 1991 Apple Computer, Inc.
  207.  
  208.  
  209. ******************************************************************
  210. Written & sent to Apple: July 24, 1989
  211. Revised: January 7, 1993
  212.  
  213. I am writing to report some errors in the otherwise helpful document:
  214. "Grayscale Information" (Rev. 23 April '89) in Tech Info.
  215.  
  216. 0. "A quick and easy way to do this is to pass -1 to the SetGamma Control parameter block as the new table address."  According to Designing Cards & Drivers, 3rd Ed., we should pass NULL, not -1.
  217.  
  218. 1. In the next to last line, it says that GetGamma is "(status call 6 on TFB rev 2 drivers and up)". This is wrong. The MPW header file Video.h correctly defines cscGetGamma = 8. 
  219.  
  220. 2. The explanation of gDataCnt is wrong. "It is always equal to 2gDataWidth..." Presumably it was meant to say 2^gDataWidth or 1<<gDataWidth. However, is this really true? There is a video card on the market now with 9-bit dacs. Does it conform to this rule?
  221.  
  222. 3. Another error in the same paragraph: "The data always appears ... in a field that is the next larger number of bytes than gDataWidth." According to this sentence, since gDataWidth is 8 bits, the data would be stored in two bytes, not one. What is intended is "larger or equal to", or to rephrase, "the smallest number of bytes that will hold gDataWidth bits".
  223.  
  224. Denis G. Pelli
  225. Institute for Sensory Research
  226. Syracuse University